﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zyg.Utility.Helper;

namespace Zyg.SSO.Core.Business
{
    public class Role : BusinessBase<Role, Guid>
    {
        #region Fiedls and Properties

        private string name = String.Empty;
        /// <summary>
        /// Gets or sets the Name of the Role.
        /// </summary>
        public string Name
        {
            get { return name; }
            set
            {
                if (name != value)
                {
                    MarkChanged("Name");
                    name = value;
                }
            }
        }

        private string remark = String.Empty;
        /// <summary>
        /// Gets or sets the Remark of the Role.
        /// </summary>
        public string Remark
        {
            get { return remark; }
            set
            {
                if (remark != value)
                {
                    MarkChanged("Remark");
                    remark = value;
                }
            }
        }

        private RoleAccountCollection accounts = null;
        /// <summary>
        /// Get or set the accounts of role.
        /// </summary>
        public RoleAccountCollection Accounts
        {
            get
            {
                if (accounts == null)
                    accounts = new RoleAccountCollection(this, RelationType.OneToMany);
                return accounts;
            }
            set
            {
                accounts = value;
                MarkChanged("Accounts");
            }
        }

        /// <summary>
        /// 获取角色的权限
        /// </summary>
        public IList<Permission> Permissions
        {
            get
            {
                return RolePermission.SelectPermissionByRoleID(this.Id);
            }
        }

        #endregion

        #region Static elements

        public static readonly Role User;

        public static readonly Role SuperAdmin;

        #endregion

        #region Constructors

        static Role()
        {
            User = new Role
            {
                Id = new Guid("0a6d3782-1db6-48e7-bbf5-3cec7bde60d7"),
                DateCreated = DateTime.Now,
                Name = "普通账户",
                Remark = "普通用户角色"
            };

            SuperAdmin = new Role
            {
                Id = new Guid("ee2586ae-87f2-42df-b412-d9c5f294bd74"),
                DateCreated = DateTime.Now,
                Name = "超级管理员",
                Remark = "超级管理员"
            };
        }

        public Role()
        {
            // Accounts = new RoleAccountCollection(this, RelationType.OneToMany);
        }

        #endregion

        #region Base overrides

        protected override void ValidationRules()
        {
            AddRule("RoleIDNotNull", "角色标识不合法", Guid.Empty == this.Id);
            AddRule("NameLengthBound", "角色名称长度必须在1与100个字符之间", !Name.IsInLengthBound(1, 100));
        }

        protected override Role DataSelect(Guid id)
        {
            if (id == Role.User.Id)
                return Role.User;
            else if (id == Role.SuperAdmin.Id)
                return SuperAdmin;
            else
                return Services.DALService.SelectRole(id);
        }

        protected override void DataUpdate()
        {
            Services.DALService.UpdateRole(this);
        }

        protected override void DataInsert()
        {
            Services.DALService.InsertRole(this);
        }

        protected override void DataDelete()
        {
            if (this.Id == Role.User.Id)
                return;

            Services.DALService.DeleteRole(this);
        }

        public override SaveAction Save()
        {
            if (this.IsDeleted && !this.IsNew)
            {
                for (int i = 0; i < this.Accounts.Count; i--)
                {
                    this.Accounts[i].RoleID = Role.User.Id;
                }

                this.Accounts.Save();
            }

            SaveAction action = base.Save();

            if (accounts != null)
                Accounts.Save();

            return action;
        }

        #endregion

        #region Static Methods

        public static bool Exist(string name)
        {
            return Services.DALService.ExistsRole(name);
        }

        public static IList<Role> GetRoles()
        {
            return Services.DALService.FillRoles();
        }

        #endregion

        #region Events

        #endregion
    }
}
